package com.xiyuan.smartutils.extend;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * the description for class ConcurrentHashSet
 *
 * @author xiyuan-lgz 2023-11-26 @version v1.0.0 新建与整理
 */
public class ConcurrentHashSet<E> extends AbstractSet<E> {
    private static final String EMPTY = "";
    private final ConcurrentHashMap<E, String> map;
    
    public ConcurrentHashSet() {
        this.map = new ConcurrentHashMap<E, String>();
    }
    
    public ConcurrentHashSet(int initialCapacity) {
        this.map = new ConcurrentHashMap<E, String>(initialCapacity);
    }
    
    public Iterator<E> iterator() {
        return this.map.keySet().iterator();
    }
    
    public int size() {
        return this.map.size();
    }
    
    public boolean isEmpty() {
        return this.map.isEmpty();
    }
    
    public boolean contains(Object o) {
        return this.map.containsKey(o);
    }
    
    public boolean add(E e) {
        return e != null && this.map.put(e, EMPTY) == null;
    }
    
    public boolean remove(Object o) {
        return o != null && this.map.remove(o) == EMPTY;
    }
    
    public void clear() {
        this.map.clear();
    }
}
